package com.syni.mdd.yhd.common.all.quartz.order.job;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrderStatus;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderStatusService;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @className GroupMessageTimingInit
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/8 18:16
 * @VERSION 1.0
 **/
@Component
@DependsOn("applicationContextHolder")
public class OrderCancelTimingInitialize {
    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Autowired
    BmsDxOrderStatusService bmsDxOrderStatusService;

    @PostConstruct
    public void init(){
        //查询所有未支付的订单id
        Integer pageNum = 1;
        Integer pageSize = 100;
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("status", Constants.NO_PAY);
        while (true) {
            Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize);
            List<BmsDxOrderStatus> bmsDxOrderStatuses = bmsDxOrderStatusService.getBaseDao().findAll(new SpecificationUtils(conditionMap),pageable).getContent();
            Set<Integer> orderIds = new HashSet<>();
            if(bmsDxOrderStatuses != null && bmsDxOrderStatuses.size() > 0){
                for(BmsDxOrderStatus bmsDxOrderStatus : bmsDxOrderStatuses) {
                    orderIds.add(bmsDxOrderStatus.getOrderId());
                }
                if(orderIds.size() > 0){
                    Map<String,Object> paramsMap = new HashMap<>();
                    paramsMap.put("id_IN",orderIds);
                    List<BmsDxOrder> bmsDxOrders = bmsDxOrderService.getBaseDao().findAll(new SpecificationUtils(paramsMap));
                    if(bmsDxOrders.size() > 0){
                        for(BmsDxOrder bmsDxOrder : bmsDxOrders){
                            if(bmsDxOrder.getOrderType() != Constants.LOCAL_LIFE_ORDER_TYPE) {
                                if (bmsDxOrder.getTimingCancelTime() == null) {
                                    bmsDxOrder.setTimingCancelTime(DateTimeUtils.addMinutes(bmsDxOrder.getNewTime(), Constants.AUTO_CANCEL_ORDER_MINUTE));
                                }
                                bmsDxOrderService.doOrderCancelTiming(OrderCancelTimingJob.class, bmsDxOrder.getTimingCancelTime(), bmsDxOrder);
                            }
                        }
                    }
                    pageNum++;
                }
            }else {
                break;
            }
        }
    }
}
